package com.tbit.uqbike.webmanager.business.user_op_log.controller;

import com.alibaba.fastjson.JSON;
import com.tbit.common.entity.UserOpLog;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.webmanager.business.user_op_log.object.dto.UserOpLogPageReq;
import com.tbit.uqbike.webmanager.business.user_op_log.service.UserOpLogService;
import com.tbit.uqbike.webmanager.dto.UserOpLogDTO;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 人员操作日志
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2024/11/26
 */
@Slf4j
@RestController
@RequestMapping("/userOpLog")
@RequiredArgsConstructor
public class UserOpLogController {
    final UserOpLogService userOpLogService;

    /**
     * 分页查询人员操作日志
     *
     * @param req 查询参数
     */
    @GetMapping("/getPage")
    public Result<PageResult<UserOpLog>> getPage(@Validated UserOpLogPageReq req) {
        if (req.getOpUser() == null && req.getOpUserPhone() == null
                && req.getTargetUser() == null && req.getTargetUserPhone() == null) {
            return Result.error("请输入操作人(手机号)或被操作人(手机号)后查询");
        }
        return Result.success(userOpLogService.getRecordList(req));
    }
    /**
     * @description: 导出人员操作日志
     * @author: mo.shanyong
     * @date: 2025/3/17 19:55
     * @param: req
     * @return: com.tbit.common.entity.view.Result
     */
    @PostMapping("/exportInfo")
    public Result exportInfo(@Validated UserOpLogPageReq req) {
        if (req.getOpUser() == null && req.getOpUserPhone() == null
                && req.getTargetUser() == null && req.getTargetUserPhone() == null) {
            return Result.error("请输入操作人(手机号)或被操作人(手机号)后查询");
        }
        req.setPageNO(1);
        //查询总数
        int count = userOpLogService.getCount(req);
        req.setRowCount(count == 0 ? 10 : count);
        log.info("exportInfo----->{}", JSON.toJSONString(req));
        PageResult<UserOpLog> recordList = userOpLogService.getRecordList(req);
        List<UserOpLogDTO> userOpLogDTOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(recordList.getList())){
            BeanUtils.copyProperties(recordList.getList(),userOpLogDTOList);
        }
        ExcelUtil<UserOpLogDTO> excel = new ExcelUtil<>(UserOpLogDTO.class);
        JsonResult jsonResult = excel.exportExcel(userOpLogDTOList, "人员操作日志");
        return Result.success(jsonResult.getData());
    }
}
